﻿using System.Collections.Generic;
using System.Linq;
using HeritageAcademy.Models.DataModels;
using HeritageAcademy.Models.Loaders;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Models.Filters
{
    public class RecentsFilter : CourseFilter
    {
        #region Parameters

        private UsersConfigLoader _usersDataModel;
        private CoursesConfigLoader _coursesDataModel;

        #endregion

        #region Ctor
        
        public RecentsFilter(LibraryViewModel library) : base(library) { }
		
        #endregion

		#region Properties 
        
        public override string DisplayText
        {
            get { return "Начатые"; }
        }

        public override bool HasSubFilters
        {
            get { return false; }
        }
		
        #endregion Properties 

		#region Public Methods 
        
        public override void RefreshResults()
        {
            _usersDataModel = new UsersConfigLoader(new Users());
            var coursesIdList = new List<string>();

            if (_usersDataModel.ActiveUser != null)
            {
                _coursesDataModel = new CoursesConfigLoader(new CoursesData());
                foreach (var course in _coursesDataModel.Model.CourseData)
                {
                    coursesIdList.AddRange(
                        course.Students.Where(student => student.Id == _usersDataModel.ActiveUser.Id).Select(
                            student => course.Id));
                }
            }

            var results = new List<CourseViewModel>();
            foreach (var book in Library.Courses)
            {
                results.AddRange(from id in coursesIdList
                                 where book.Model.Identifier != null && book.Model.Identifier == id
                                 select book);
            }

            //var results = (from book in Library.Books
            //               where book.Model.Identifier != null && book.Model.Identifier == DateTime.MinValue
            //               //orderby book.Model.UserMetadata.LastAccess descending
            //               select book)
            //               .Take(5);

            Results = new List<CourseViewModel>(results);
        }
		
        #endregion Public Methods 
    }
}
